home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / NVRAM.C < prev    next >
C/C++ Source or Header  |  1996-09-03  |  6KB  |  387 lines

  1. /* Non-Volatile Ram user commands
  2.  * Copyright 1993 Brian A. Lantz, KO4KS
  3.  */
  4. #include "global.h"
  5. #include "commands.h"
  6. #include "mbuf.h"
  7. #include "internet.h"
  8. #include "timer.h"
  9. #include "netuser.h"
  10. #include "iface.h"
  11. #include "session.h"
  12. #include "ip.h"
  13.  
  14. #if !defined(_lint)
  15. static char rcsid[] OPTIONAL = "$Id: nvram.c,v 1.10 1996/09/04 01:34:13 root Exp root $";
  16. #endif
  17.  
  18. #define old
  19.  
  20. extern uint32 Ip_addr;
  21. extern char Mycall[];
  22. #ifdef nope
  23. static int doipadd (int argc,char *argv[],void *p);
  24. #endif
  25. static int doread (int argc,char *argv[],void *p);
  26. static int dowrite (int argc,char *argv[],void *p);
  27.  
  28. void PutNV (int value);
  29. int readit (int address, int single);
  30. int writeit (int address, int value);
  31.  
  32. static struct cmds nvcmds[] = {
  33. #ifdef nope
  34.     "ipaddress",      doipaddr,       0,      0, NULLCHAR,
  35. #endif
  36.     "read",           doread,         0,      0, NULLCHAR,
  37. #ifndef old
  38.     "write",          dowrite,        0,      0, NULLCHAR,
  39. #endif
  40.     NULLCHAR,
  41. };
  42.  
  43. int
  44. donv(argc,argv,p)
  45. int argc;
  46. char *argv[];
  47. void *p;
  48. {
  49.     return subcmd(nvcmds,argc,argv,p);
  50. }
  51.  
  52. #ifdef nope
  53. static int
  54. doipaddr(argc,argv,p)
  55. int argc;
  56. char *argv[];
  57. void *p;
  58. {
  59. int32 n;
  60.  
  61.     if(argc < 2) {
  62.         tprintf("%s\n",inet_ntoa(Ip_addr));
  63.     } else if((n = resolve(argv[1])) == 0){
  64.         tprintf(Badhost,argv[1]);
  65.         return 1;
  66.     } else
  67.         Ip_addr = n;
  68.     return 0;
  69. }
  70.  
  71. #endif
  72.  
  73. static int
  74. doread(argc,argv,p)
  75. int argc;
  76. char *argv[];
  77. void *p;
  78. {
  79. #ifdef old
  80. char *n = 0x3f8;
  81. #endif
  82. int c;
  83. char buf[8], *newargv[3];
  84.  
  85. #ifdef old
  86.     c = *n;
  87. #else
  88.     if ((c = readit (0, 1)) == -1)    {
  89.         tprintf ("NV Read Error\x07\n");
  90.         return 0;
  91.     }
  92. #endif
  93.     Ip_addr += (c << 8);
  94.     sprintf (buf, "unit%-.2d", c);
  95.     setcall(Mycall,buf);
  96.     newargv[1] = buf;
  97.     newargv[0] = newargv[2] = NULLCHAR;
  98.     dohostname (2, newargv, (void *) 0);
  99.     tprintf ("NV Ram Read\n");
  100.     return 0;
  101. }
  102.  
  103.  
  104. #ifndef old
  105. static int
  106. dowrite(argc,argv,p)
  107. int argc;
  108. char *argv[];
  109. void *p;
  110. {
  111. int c;
  112.  
  113. /* takes unit # from Ip_addr, for now */
  114.  
  115.     c = ((Ip_addr >> 8) & 0x0000ff);
  116.     if (writeit (0, c))
  117.         tprintf ("NV Ram Write Error\x07\n");
  118.     else
  119.         tprintf ("NV Ram Write Complete\n");
  120.     return 0;
  121. }
  122.  
  123.  
  124. #asm
  125. Base    equ    $700000
  126. PACNT    equ    Base+$81e
  127. PADDR    equ    Base+$820
  128. PADAT    equ    Base+$822
  129. PBCNT    equ    Base+$824
  130. PBDDR    equ    Base+$826
  131. PBDAT    equ    Base+$828
  132.  
  133. Wait:    nop
  134.     nop
  135.     nop
  136.     dbra    d0,Wait
  137.     rts
  138.  
  139. GetNVAck:
  140.     bclr    #0,PBDDR+1    set it for input
  141.     move.l    #20,d0
  142.     bsr    Wait
  143.     bsr    CLKhi
  144.     move.l    #20,d0
  145.     bsr    Wait
  146.     move.b    PBDAT+1,d1
  147.     bset    #0,PBDDR+1    set it for output
  148.     move.l    #20,d0
  149.     bsr    Wait
  150.     bsr    CLKlo
  151.     btst    #0,d1
  152.     bne    GetAk1
  153. #ifdef DIAG
  154.     lea.l    ACK(pc),a0
  155.     move.l    a0,d0
  156.     bsr    puts
  157. #endif
  158.     clr.l    d0
  159.     rts
  160.  
  161. GetAk1
  162. #ifdef DIAG
  163.     lea.l    NOACK(pc),a0
  164.     move.l    a0,d0
  165.     bsr    puts
  166. #endif
  167.     move.l    #-1,d0
  168.     rts
  169.  
  170. DATAlo:    bclr    #0,PBDAT+1
  171.     move.l    #20,d0
  172.     bra    Wait
  173.  
  174. DATAhi:    bset    #0,PBDAT+1
  175.     move.l    #20,d0
  176.     bra    Wait
  177.  
  178. CLKlo:    bclr    #4,PADAT
  179.     move.l    #20,d0
  180.     bra    Wait
  181.  
  182. CLKhi:    bset    #4,PADAT
  183.     move.l    #20,d0
  184.     bra    Wait
  185.  
  186. #endasm
  187.  
  188. start ()
  189. {
  190. #ifdef DIAG
  191.     puts ("Start bit being sent");
  192. #endif
  193.     DATAhi();
  194.     CLKhi();
  195.     DATAlo();
  196.     CLKlo();
  197. #ifdef DIAG
  198.     puts ("Complete!");
  199. #endif
  200. }
  201.  
  202. stop ()
  203. {
  204. #ifdef DIAG
  205.     puts ("Stop bit being sent");
  206. #endif
  207.     DATAlo();
  208.     CLKhi();
  209.     DATAhi();
  210.     CLKlo();
  211. #ifdef DIAG
  212.     puts ("Complete!\n");
  213. #endif
  214. }
  215.  
  216. clockbit ()
  217. {
  218.     CLKhi();
  219.     Wait (20);
  220.     CLKlo();
  221. }
  222.  
  223. zero ()
  224. {
  225.     DATAlo();
  226. #ifdef DIAG
  227.     putchar ('0');
  228. #endif
  229.     clockbit();
  230. }
  231.  
  232. one ()
  233. {
  234.     DATAhi();
  235. #ifdef DIAG
  236.     putchar ('1');
  237. #endif
  238.     clockbit();
  239. }
  240.  
  241. void
  242. PutNV (value)
  243. int value;
  244. {
  245. int bitmask = 0x80, count;
  246.  
  247.     for (count = 0; count < 8; count++)    {
  248.         if (value & bitmask)
  249.             one();
  250.         else
  251.             zero();
  252.         bitmask >>= 1;
  253.     }
  254. }
  255.  
  256. int
  257. getbit()
  258. {
  259. #asm
  260.     move.b    PBDAT+1,d1
  261.     and.b    #$fe,d1
  262. #ifdef nope
  263.     move.l    #20,d0
  264.     bsr    Wait
  265. #endif
  266.     move.l    d1,d0
  267. #endasm
  268. }
  269.  
  270.  
  271. int
  272. GetNV ()
  273. {
  274. int bits, count;
  275. int value = 0;
  276.  
  277. @    bclr    #0,PBDDR+1    /* set it for input */
  278.     Wait(20);
  279.     for (count = 0; count < 8; count++)    {
  280.         CLKhi();
  281.         value >>= 1;
  282. /*        Wait (20);        */
  283.         bits = getbit();
  284.         if (bits)    {
  285. #ifdef DIAG
  286.             putchar ('1');
  287. #endif
  288.             value |= 0x80;
  289.         } else    {
  290. #ifdef DIAG
  291.             putchar ('0');
  292. #endif
  293.         }
  294.         CLKlo();
  295. /*        Wait (20);        */
  296.     }
  297. @    bset    #0,PBDDR+1    /* set it for output */
  298.     putchar ('\n');
  299.     return (value);
  300. }
  301.  
  302. int
  303. readit (address, single)
  304. int address, single;
  305. {
  306. int value;
  307.  
  308.     start();
  309.     PutNV (0xa0);
  310.     if (GetNVAck () == -1)
  311.         return (-1);
  312.     PutNV (address);
  313.     if (GetNVAck () == -1)
  314.         return (-1);
  315.     start();
  316.     PutNV (0xa1);
  317.     if (GetNVAck () == -1)
  318.         return (-1);
  319.     value = GetNV ();
  320.     if (single)
  321.         stop();
  322. #ifdef DIAG
  323.     printf ("Value of NV Ram at that location: %02x\n", value);
  324. #endif
  325.     return (value);
  326. }
  327.  
  328.  
  329. int
  330. writeit (address, value)
  331. int address;
  332. int value;
  333. {
  334.     start();
  335.     PutNV (0xa0);
  336.     if (GetNVAck () == -1)
  337.         return (-1);
  338.     PutNV (address);
  339.     if (GetNVAck () == -1)
  340.         return (-1);
  341.     PutNV (value);
  342.     if (GetNVAck () == -1)
  343.         return (-1);
  344.     stop();
  345.     return (0);
  346. }
  347.  
  348.  
  349.  
  350. #asm
  351. EnNV    bclr    #4,PACNT    make sure PA12 is enabled
  352.     bset    #4,PADDR    set it for output
  353.     bclr    #0,PBCNT+1    make sure that PB0 is enabled
  354.     bset    #0,PBDDR+1    set it for output
  355.     move.l    #20,d0
  356.     bra    Wait
  357.  
  358. #endasm
  359.  
  360. void
  361. AckIt()
  362. {
  363.     DATAlo ();
  364.     CLKhi ();
  365.     CLKlo ();
  366. }
  367.  
  368.  
  369. int
  370. readall (buf)
  371. unsigned char *buf;
  372. {
  373. int c;
  374.  
  375.     c = readit (0, 0);
  376.     if (c == -1)
  377.         return (-1);
  378.     buf[0] = c;
  379.     for (c = 1; c < 256; c++)    {
  380.         AckIt ();
  381.         buf[c] = GetNV();
  382.     }
  383.     stop ();
  384.     return (0);
  385. }
  386. #endif
  387.